package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.UserThirdMapper;
import com.example.demo.module.UserBean;
import com.example.demo.module.UserDetailBean;
import com.example.demo.module.UserThirdBean;
import com.example.demo.module.UserThirdBeanVo;
import com.example.demo.oauth.OauthQQ;
import com.example.demo.oauth.OauthSina;
import com.example.demo.oauth.OauthWeixin;
import com.example.demo.service.UserDetailService;
import com.example.demo.service.UserService;
import com.example.demo.service.UserThirdService;
import com.example.demo.util.GuidUtils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service("userThirdService")
public class UserThirdServiceImpl implements UserThirdService {

	@Autowired
	private UserThirdMapper thirdMapper;

	@Autowired
    private UserService userService;

	@Autowired
    private UserDetailService userDetailService;

	@Autowired
    private UserThirdService userThirdService;

	@Override
	public UserThirdBean selectById(String id) {
		return null;
	}

	@Override
	public void deleteById(String id) {

	}

	@Override
	public int update(UserThirdBean userThirdBean) {
		return 0;
	}

	@Override
	public int add(UserThirdBean userThirdBean) {
		return thirdMapper.add(userThirdBean);
	}

	@Override
	public List<UserThirdBean> findAll() {
		return null;
	}

	@Override
	public UserBean sinaCallback(String code) {
		OauthSina sina = OauthSina.me();
		JSONObject jsonObject = sina.getUserInfoByCode(code);
		String openId = jsonObject.getString("id");
		UserBean user = userService.selectUserIsThird(openId);
		if(ObjectUtils.isEmpty(user)){
			user = userService.selectByUserBind(openId,null);
			if(ObjectUtils.isEmpty(user)) {
				String nickName = jsonObject.getString("name");
				String headPhoto = jsonObject.getString("profile_image_url");
				String gender = jsonObject.getString("gender");
				userThirdService.add(new UserThirdBean(GuidUtils.getUuid(), 2, openId, nickName, headPhoto));
				int sex = 1;
				if ("m".equals(gender)) {
					sex = 0;
				}
				String uuid = GuidUtils.getUuid();
				userService.add(new UserBean(uuid, uuid, openId, 000000));
				userDetailService.add(new UserDetailBean(GuidUtils.getUuid(), uuid, nickName,
						sex, headPhoto, openId));
//			user.setId(uuid);
				user = userService.selectUserIsThird(openId);
			}else {
			    return user;
			}
		}
		return user;
	}

	@Override
	public UserBean weiXinCallback(String code) {
		OauthWeixin ow = OauthWeixin.me();
		String token =  ow.getTokenByCode(code);
		Gson gson = new Gson();
		UserThirdBeanVo userThirdBeanVo1 = gson.fromJson(token,UserThirdBeanVo.class);
		String openId = userThirdBeanVo1.getOpenid();
		String a = ow.getUserInfo(userThirdBeanVo1.getAccess_token(),openId);
		UserThirdBeanVo userThirdBeanVo = gson.fromJson(a,UserThirdBeanVo.class);
		//微信多应用唯一标识
		String unionid = userThirdBeanVo.getUnionid();
		UserBean user = userService.selectUserIsThird(unionid);
		if(ObjectUtils.isEmpty(user)){
			user = userService.selectByUserBind(openId,null);
			if(ObjectUtils.isEmpty(user)) {
				String nickName = userThirdBeanVo.getNickname();
				String headPhoto = userThirdBeanVo.getHeadimgurl();
				int gender = userThirdBeanVo.getSex();
				userThirdService.add(new UserThirdBean(GuidUtils.getUuid(), 1, unionid, nickName, headPhoto));
				int sex = 1;
				if (gender == 1) {
					sex = 0;
				}
				String uuid = GuidUtils.getUuid();
				userService.add(new UserBean(uuid, uuid, unionid, 000000));
				userDetailService.add(new UserDetailBean(GuidUtils.getUuid(), uuid, nickName,
						sex, headPhoto, unionid));
//			user.setId(uuid);
				user = userService.selectUserIsThird(unionid);
			}else{
				return user;
			}
		}
		return user;
	}

	@Override
	public UserBean qqCallback(String code) {
		OauthQQ qq = OauthQQ.me();
		JSONObject jsonObject = qq.getUserInfoByCode(code);
		String openId = jsonObject.getString("openid");
		UserBean user = userService.selectUserIsThird(openId);
		if(ObjectUtils.isEmpty(user)){
			user = userService.selectByUserBind(openId,null);
			if(ObjectUtils.isEmpty(user)) {
				String nickName = jsonObject.getString("nickname");
				String headPhoto = jsonObject.getString("figureurl_qq_1");
				String gender = jsonObject.getString("gender");
				String uuid = GuidUtils.getUuid();
				userThirdService.add(new UserThirdBean(GuidUtils.getUuid(), 3, openId, nickName, headPhoto));
				int sex = 1;
				if ("男".equals(gender)) {
					sex = 0;
				}
				userService.add(new UserBean(uuid, uuid, openId, 000000));
				userDetailService.add(new UserDetailBean(GuidUtils.getUuid(), uuid, nickName,
						sex, headPhoto, openId));
				//user.setId(uuid);
				user = userService.selectUserIsThird(openId);
			}else{
				return user;
			}
		}
		return user;
	}
}
